/**
 * YIXUN_2.0
 */
package com.xiongyingqi.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 计时器
 * 
 * @author 瑛琪
 * @version 2013-7-26 下午6:05:52
 */
public class TimerHelper {
	private static Map<Object, Long> holderObjects = new HashMap<Object, Long>();

	/**
	 * 获取与上个计时点以毫秒为单位的时差 ，如果是第一个计时点，则开始即时并返回0<br>
	 * 2013-7-26 下午6:21:40
	 * 
	 * @return long
	 */
	public static long getTime() {
		// StackTraceHelper.printStackTrace();
		StackTraceElement[] stackTraceElements = StackTraceHelper
				.getStackTrace();
		StackTraceElement stackTraceElement = stackTraceElements[2]; // 调用本类的对象类型堆栈
		String clazz = stackTraceElement.getClassName(); // 调用本类的对象类型
		// String methodName = stackTraceElement.getMethodName();
		// int lineNumber = stackTraceElement.getLineNumber();
		// long hashCode = 17;
		// hashCode = 37 * hashCode + clazz.hashCode();
		// hashCode = 37 * hashCode + methodName.hashCode();
		// hashCode = 37 * hashCode + lineNumber;
		// System.out.println("stackTraceElement.getClassName() ========= " +
		// stackTraceElement.getClassName());
		Long startTime = holderObjects.get(clazz);
		// System.out.println(stackTraceElement);
		if (startTime == null) {
			startTime = System.currentTimeMillis();
		}
		long time = System.currentTimeMillis() - startTime;
		startTime = System.currentTimeMillis();
		holderObjects.put(clazz, startTime);
		return time;
	}

	public static void main(String[] args) {
		System.out.println(getTime());
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(getTime());
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(getTime());
	}
}
